#include "Poly.h"

/*
 *	Poly constructor with indiv. points
 */

Poly::Poly(Ogre::String name, bool visible, Ogre::Vector2 p1, Ogre::Vector2 p2, Ogre::Vector2 p3, Ogre::Vector2 p4, Ogre::SceneManager *sceneManager)
{
	m_polyName = name;
	m_draw = visible;
	m_sceneManager = sceneManager;

	m_polygonPoints.push_back(p1);
	m_polygonPoints.push_back(p2);
	m_polygonPoints.push_back(p3);
	m_polygonPoints.push_back(p4);
	m_polygonPoints.push_back(p1);

	drawSelf();
}

Poly::Poly(Ogre::String name, Ogre::Vector2 position, Ogre::Real depth, Ogre::Real rotation, bool visible, int type, std::vector<Ogre::Vector2> polyPoints, Ogre::SceneManager *sceneManager)
{
	m_polyName = name;
	m_draw = visible;
	m_sceneManager = sceneManager;
	m_polyType = type;
	m_polyCenter.x = position.x;
	m_polyCenter.y = position.y;
	m_rotation = rotation;
	m_selected = false;
	m_depth = depth;

	for (int i = 0; i < polyPoints.size(); i++)
	{
		m_polygonPoints.push_back(polyPoints.at(i));
	}

	drawSelf();
}

/*
 *	Poly Destructor
 */

Poly::~Poly(void){}

void Poly::setPosition(Ogre::Vector2 newPosition){

	Ogre::Vector2 dispVector = newPosition - m_polyCenter;
	m_polyCenter = newPosition;

	//adjust own collision vectors
	for(int i=0; i < m_polygonPoints.size(); i++){
		m_polygonPoints.at(i).x += dispVector.x;
		m_polygonPoints.at(i).y += dispVector.y;
	}

	//adjust node position
	m_polyNode->setPosition(newPosition.x, newPosition.y, m_depth);
}

Ogre::Vector2 Poly::getPosition(){
	return m_polyCenter;
}

void Poly::drawSelf()
{
	Ogre::Entity* polyMesh = m_sceneManager->createEntity("cube.mesh");

	polyMesh->setMaterialName("MMI/Crate");
	polyMesh->setCastShadows(true);

	m_polyNode = m_sceneManager->getRootSceneNode()->createChildSceneNode();
	m_polyNode->attachObject(polyMesh);
	m_polyNode->scale(0.8,0.8,0.8);
	m_polyNode->setPosition(Ogre::Vector3(m_polyCenter.x,m_polyCenter.y,-130));
	m_polyNode->roll(Ogre::Degree(m_rotation));
}

void Poly::makeDynamic(bool dynamic){
	Ogre::Entity* polyMesh = static_cast<Ogre::Entity*>(m_polyNode->getAttachedObject(0));
	if(dynamic){
		//change type
		m_polyType = DYNAMIC;

		//change material
		polyMesh->setMaterialName("MMI/Crate2");
	}
	else{
		//change type
		m_polyType = STATIC;

		//change material
		polyMesh->setMaterialName("MMI/Crate");
	}

}